#!/usr/bin/perl
use strict;

my $eth = $ARGV[0];

my $time = time;

sub get_in_out {
	my ($last_in, $last_out);
	my $content = `ifconfig $eth | grep "RX bytes"`;
	chomp $content;
	if ($content =~ /RX bytes\:(\d+).+TX bytes\:(\d+) \(/) {
		($last_in, $last_out) =	($1, $2);
	}
	return  ($last_in, $last_out);
}

while (1) {
	my ($old_in, $old_out) = get_in_out();
	my $old_time = time;
	sleep(1);
	my ($new_in, $new_out) = get_in_out();
	my $new_time = time;
	my $speed_in = sprintf("%.2f", ($new_in - $old_in) / 1024 / ($new_time - $old_time));
	my $speed_out = sprintf("%.2f", ($new_out - $old_out) / 1024 / ($new_time - $old_time));
	print "speed_in:$speed_in, speed_out:$speed_out\n";
}

exit;


